package com.naive.imperative.program;

import java.util.LinkedList;
import java.util.List;

public class Program {
	private OperationBlock rootContainer;
	private final List<ProgramListener> listeners = new LinkedList<ProgramListener>();
	
	public void addProgramListener(ProgramListener pl) {
		listeners.add(pl);
	}
	public void removeProgramListener(ProgramListener pl) {
		listeners.remove(pl);
	}
	
	public EditContext prepareChange(Operation op) {
		return new EditHandle(op);
	}
	
	public OperationBlock getRootContainer() {
		return rootContainer;
	}
	
	private class EditHandle implements EditContext {
		private Operation target;
		
		public EditHandle(Operation target) {
			this.target = target;
		}

		public void beforeSet(int key, 
				EvaluatableOperation<?> newV) {
		}
		
		public void beforeAdd(Block b, int index, Operation ...operations) {
		}

		public void beforeRemove(Block b, Operation ...operations) {
		}
		
		public Operation getTarget() {
			return target;
		}
		
		public void commit() {
			ChangeEvent che = new ChangeEvent(target);
			
			for(ProgramListener pl:listeners) {
				pl.operationChanged(e);
			}
		}
	}

}
